package com.zuokun.service.impl;

import com.zuokun.manager.RoomManager;
import com.zuokun.mapper.RoomMapper;
import com.zuokun.protol.MsgBase;
import com.zuokun.protol.room.*;
import com.zuokun.service.RoomService;
import com.zuokun.vo.PlayerInfo;
import com.zuokun.vo.RoomInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询房间列表
     *
     * @param msgGetRoomList
     * @return
     */
    public MsgGetRoomList getRoomList(MsgGetRoomList msgGetRoomList) {
        MsgGetRoomList result = new MsgGetRoomList();
        List<RoomInfo> rooms = new ArrayList<>(RoomManager.roomMap.values());
        result.setRooms(rooms);
        return result;
    }

    /**
     * 创建房间
     *
     * @param msgCreateRoom
     * @return
     */
    @Override
    public MsgCreateRoom createRoom(MsgCreateRoom msgCreateRoom) {
        //创建返回结果对象
        MsgCreateRoom result = new MsgCreateRoom();
        if(msgCreateRoom.getPlayerInfo() == null){
            result.setResult(false);
            result.setMessage("创建房间的玩家不存在！");
        }else {
            if(msgCreateRoom.getPlayerInfo().getRoomId() != null){
                result.setResult(false);
                result.setMessage("该玩家已处于房间中！");
            }
            //新增房间
            RoomInfo roomInfo = RoomManager.addRoom();
            msgCreateRoom.setResult(roomInfo.addPlayer(msgCreateRoom.getPlayerInfo()));
        }
        return msgCreateRoom;
    }

    /**
     * 离开房间
     *
     * @param msgLeaveRoom
     * @return
     */
    @Override
    public MsgLeaveRoom leaveRoom(MsgLeaveRoom msgLeaveRoom) {
        //设置返回参数
        MsgLeaveRoom result = new MsgLeaveRoom();
        //根据玩家信息获取房间
        PlayerInfo playerInfo = msgLeaveRoom.getPlayerInfo();
        Integer roomId = playerInfo.getRoomId();
        RoomInfo room = RoomManager.getRoom(roomId);
        //将玩家从房间移除
        result.setResult(room.removePlayer(playerInfo));
        return result;
    }

    /**
     * 进入房间
     *
     * @param msgEnterRoom
     * @return
     */
    @Override
    public MsgBase enterRoom(MsgEnterRoom msgEnterRoom) {
        //获得返回结果
        MsgEnterRoom result = new MsgEnterRoom();
        //根据房间id获得房间
        RoomInfo room = RoomManager.getRoom(msgEnterRoom.getId());
        //将玩家加入房间
        result.setResult(room.addPlayer(msgEnterRoom.getPlayerInfo()));
        return result;
    }

    /**
     * 获取房间信息
     *
     * @param msgGetRoomInfo
     * @return
     */
    @Override
    public MsgBase getRoomInfo(MsgGetRoomInfo msgGetRoomInfo) {
        MsgGetRoomInfo result = new MsgGetRoomInfo();
        PlayerInfo playerInfo = msgGetRoomInfo.getPlayerInfo();
        Integer roomId = playerInfo.getRoomId();
        RoomInfo room = RoomManager.getRoom(roomId);
        List<PlayerInfo> playerList = room.getPlayerList();
        PlayerInfo[] playerInfos = playerList.toArray(new PlayerInfo[playerList.size()]);
        result.setPlayers(playerInfos);
        return result;
    }
}
